// AnsiTerminalDoc.cpp : implementation of the CAnsiTerminalDoc class
//

#include "stdafx.h"
#include "AnsiTerminal.h"

#include "AnsiTerminalDoc.h"
#include "DlgShowText.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAnsiTerminalDoc

IMPLEMENT_DYNCREATE(CAnsiTerminalDoc, CDocument)

BEGIN_MESSAGE_MAP(CAnsiTerminalDoc, CDocument)
	//{{AFX_MSG_MAP(CAnsiTerminalDoc)
	ON_COMMAND(ID_CONNECT, OnConnect)
	ON_UPDATE_COMMAND_UI(ID_CONNECT, OnUpdateConnect)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
	ON_COMMAND(ID_WRITE_LOG, OnWriteLog)
	ON_COMMAND(ID_CLEAR_LOG, OnClearLog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CAnsiTerminalDoc, CDocument)
	//{{AFX_DISPATCH_MAP(CAnsiTerminalDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//      DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IAnsiTerminal to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {E3799C66-C643-40F2-89A1-5185ECEDD66A}
static const IID IID_IAnsiTerminal =
{ 0xe3799c66, 0xc643, 0x40f2, { 0x89, 0xa1, 0x51, 0x85, 0xec, 0xed, 0xd6, 0x6a } };

BEGIN_INTERFACE_MAP(CAnsiTerminalDoc, CDocument)
	INTERFACE_PART(CAnsiTerminalDoc, IID_IAnsiTerminal, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnsiTerminalDoc construction/destruction

CAnsiTerminalDoc::CAnsiTerminalDoc() : 
	m_telnetLayerEvents(this), 
	m_ansiLayer(&m_ansiScreen),
	m_bbsLayer(&m_telnetLayer, &m_ansiScreen, &m_keyboard)
{
	// TODO: add one-time construction code here

	EnableAutomation();

	AfxOleLockApp();

	m_telnetLayer.RegisterEvents(&m_telnetLayerEvents);
	m_telnetLayer.SetCharStreamFilter(&m_ansiLayer);

	//m_telnetLayer.SetTelnetLogger(&m_telnetLogger);
	
	m_keyboard.RegisterEvents(&m_telnetLayer);
	m_telnetLayer.SetKeyMap(&m_keyMap);
	m_mouse.RegisterEvents(&m_bbsLayer);
}

CAnsiTerminalDoc::~CAnsiTerminalDoc()
{
	AfxOleUnlockApp();
}

BOOL CAnsiTerminalDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CAnsiTerminalDoc serialization

void CAnsiTerminalDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CAnsiTerminalDoc diagnostics

#ifdef _DEBUG
void CAnsiTerminalDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CAnsiTerminalDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAnsiTerminalDoc commands

void CAnsiTerminalDoc::OnConnect() 
{
	if(m_telnetLayer.IsConnected())
	{
		m_telnetLayer.Disconnect();
	}
	else
	{
		try
		{
			m_telnetLayer.Connect("wforum.zixia.net");
			//m_telnetLayer.Connect("59.66.70.99");
			//m_telnetLayer.Connect("bbs4.newsmth.net");
			//m_telnetLayer.Connect("bbs.zsuninet.com");
			//m_telnetLayer.Connect("bbs.gznet.edu.cn");
			//m_telnetLayer.Connect("lqqm.net");
			//m_telnetLayer.Connect("localhost", 2323);
			//m_telnetLayer.Connect("localhost", 23);
		}
		catch(const exception& e)
		{
			AfxMessageBox(e.what());
		}
	}
}

void CAnsiTerminalDoc::OnUpdateConnect(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_telnetLayer.IsConnected());
}

void CAnsiTerminalDoc::OnAppAbout() 
{
	CDlgShowText dlg;
	dlg.m_text = m_ansiScreen.GetScreenText();
	dlg.DoModal();
}

void CAnsiTerminalDoc::OnFilePrintPreview() 
{
	//
}

void CAnsiTerminalDoc::OnFilePrint() 
{
}

void CAnsiTerminalDoc::ShowLog()
{
	CStdioFile f("c:\\TelnetLog.html", CFile::modeCreate|CFile::modeWrite);
	CString s = m_telnetLogger.GetAsHtml();
	f.Write(s, s.GetLength());
	f.Close();
}

void CAnsiTerminalDoc::OnWriteLog() 
{
	ShowLog();
}

CSessionOptions& CAnsiTerminalDoc::GetOptions()
{
	return m_options;
}

CAnsiScreen& CAnsiTerminalDoc::GetScreen()
{
	return m_ansiScreen;
}

IVirtualKeyboard* CAnsiTerminalDoc::GetKeyboard()
{
	return &m_keyboard;
}

void CAnsiTerminalDoc::OnClearLog() 
{
	m_telnetLogger.Clear();
}

IVirtualMouse* CAnsiTerminalDoc::GetMouse()
{
	return &m_mouse;
}

CTelnetLayer& CAnsiTerminalDoc::GetTelnetLayer()
{
	return m_telnetLayer;
}

void CAnsiTerminalDoc::ReConnect()
{
	OnConnect();
}